﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BahdlClassLib;
using BusinessLayer;

namespace PayRoll
{
    public partial class CompanyInfo : CustomFormTemplate
    {
        public CompanyInfo()
        {
            InitializeComponent();
        }

        private void CompanyInfo_Load(object sender, EventArgs e)
        {
            this.Left = 0;
            this.Top = 0;
            this.Tag = "view";
            aniCustomTextCompanyName.Text = "";

            base.ToolBarButtonClick +=new ToolBarButtonClickEventHandler(CompanyInfo_ToolBarButtonClick);
            base.EnableControls +=new EnableControlsEventHandler(CompanyInfo_EnableControls);
            base.KeyDown += new KeyEventHandler(CompanyInfo_KeyDown);

        }

        void CompanyInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Escape)
            {
                if (this.Tag.ToString() == "view")
                {
                    CompanyInfo_ToolBarButtonClick("exit");
                }
                else
                {
                    CompanyInfo_ToolBarButtonClick("revert");
                }
            }
        }

        void CompanyInfo_EnableControls(bool Enable)
        {
            panel2.Enabled = Enable;
        }

        void CompanyInfo_ToolBarButtonClick(string Button)
        {
            if (CommonFunction.InList(Button, "add", "edit", "delete") == true)
            {
                if ((Button == "edit" || Button == "delete"))
                {
                    if (aniCustomTextCompanyName.Text == "")
                    {
                        return;
                    }
                }
            }
            switch (Button)
            {
                case "exit":
                    if (UIFunction.SureToExit("") == true)
                    {
                        ClearError();
                        this.Close();
                    }
                    break;
                case "revert":
                    ClearError();
                    this.Tag = "view";
                    CompanyInfo_EnableControls(false);
                    this.ToolBarEnterEditMode = false;
                    break;
                case "print":
                    //CrytalEditor newObject = new CrytalEditor();
                    //DataSet dsLocal = new DataSet();
                    //string CompanyDescr = getCompanyData();
                    //CompanyDescr = CompanyDescr + "Raw Material Checklist";
                    //try {
                    //    OpenBlClass();
                    //    dsLocal.Tables.Add(blRM.GetDataTable("Select * From Mst_Authority where Cocd = " + objCompany.Code + " Order by Descr", "mst_Approval"));
                    //    newObject.LoadPreviewer("Rep_Mst_Approval.rpt", dsLocal, enmReportType.FAMasters, CompanyDescr);
                    //} catch {
                    //} finally {
                    //    CloseBlClass();
                    //    newObject = null;
                    //    dsLocal = null;
                    //}
                    break;
                case "find":
                    ClearError();
                    DataSet dsLocal = new DataSet();
                    Popup popup = new Popup();
                    try
                    {
                        ClsCompanyInfo ObjClsCompanyInfo = new ClsCompanyInfo();
                        dsLocal.Tables.Add(ObjClsCompanyInfo.GetAllCompanyInfo());
                        dsLocal.Tables[0].TableName = "popup";
                        popup.Width = 50;
                        popup.ShowPopup(null, "CompanyInfoID", ref dsLocal, "popup", "CompanyName", "", "CompanyName Master", "CompanyName", "10");
                        if (!string.IsNullOrEmpty(CommonFunction.NullToString(popup.ReturnValue())))
                        {
                            uiGetData(Convert.ToInt32(popup.Field("CompanyInfoID")));
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    finally
                    {
                        dsLocal = null;
                        popup = null;
                    }
                    break;
                case "add":
                    ClearError();
                    if (uiGetData(0))
                    {
                        this.ToolBarEnterEditMode = true;
                        this.Tag = "add";
                        CompanyInfo_EnableControls(true);
                        aniCustomTextCompanyName.Select();
                    }
                    break;
                case "edit":
                    ClearError();
                    this.ToolBarEnterEditMode = true;
                    this.Tag = "edit";
                    CompanyInfo_EnableControls(true);
                    aniCustomTextCompanyName.Select();
                    break;
                case "save":
                    if (!ValidateName())
                        return;
                    if (uiPutData() == true & this.Tag.ToString() == "edit")
                    {
                        this.Tag = "view";
                        this.ToolBarEnterEditMode = false;
                        CompanyInfo_EnableControls(false);
                    }
                    break;
                case "delete":
                    ClearError();
                    uiDeleteData();
                    break;
            }
        }

        private void uiDeleteData()
        {
            try
            {
                ClsCompanyInfo ObjClsCompanyInfo = new ClsCompanyInfo();
                ObjClsCompanyInfo.CompanyInfoID = Convert.ToInt32(aniCustomTextCompanyName.Tag);
                ObjClsCompanyInfo.DeleteCompanyInfo();
                uiGetData(0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
        }
        private bool uiPutData()
        {
            

            bool functionReturnValue = false;
            try
            {
                ClsCompanyInfo ObjClsCompanyInfo = new ClsCompanyInfo();
                ObjClsCompanyInfo.CompanyName = aniCustomTextCompanyName.Text;
                ObjClsCompanyInfo.Address1 = txtAddress1.Text;
                ObjClsCompanyInfo.Address2 = txtAddress2.Text;
                ObjClsCompanyInfo.City = txtCity.Text;
                ObjClsCompanyInfo.State = txtState.Text;
                ObjClsCompanyInfo.ZipCode = txtZip.Text;
                ObjClsCompanyInfo.MobileNumber = txtMobile.Text;
                ObjClsCompanyInfo.TelephoneNumber = txtTelephone.Text;
                ObjClsCompanyInfo.FaxNumber = txtFax.Text;
                ObjClsCompanyInfo.Email = txtEmail.Text;


                if (aniCustomTextCompanyName.Tag.ToString() != string.Empty)
                    ObjClsCompanyInfo.CompanyInfoID = Convert.ToInt32(aniCustomTextCompanyName.Tag);
                ObjClsCompanyInfo.Address1 = txtAddress1.Text;
                ObjClsCompanyInfo.Address2 = txtAddress2.Text;
                ObjClsCompanyInfo.City = txtCity.Text; 
                ObjClsCompanyInfo.State = txtState.Text;
                ObjClsCompanyInfo.ZipCode = txtZip.Text;
                ObjClsCompanyInfo.MobileNumber = txtMobile.Text;
                ObjClsCompanyInfo.TelephoneNumber = txtTelephone.Text;
                ObjClsCompanyInfo.FaxNumber = txtFax.Text;
                ObjClsCompanyInfo.Email = txtEmail.Text;

                if (ObjClsCompanyInfo.SaveCompanyInfo(this.Tag.ToString()))
                    functionReturnValue = true;
                uiGetData(0);
                if (this.Tag.ToString() == "add")
                {
                    aniCustomTextCompanyName.Select();
                    txtAddress1.Select();
                    txtAddress2.Select();
                    txtCity.Select();
                    txtState.Select();
                    txtZip.Select();
                    txtMobile.Select();
                    txtTelephone.Select();
                    txtFax.Select();
                    txtEmail.Select();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
            return functionReturnValue;
        }
        private bool uiGetData(int KeyField)
        {
            bool functionReturnValue = false;
            try
            {
                ClsCompanyInfo ObjClsCompanyInfo = new ClsCompanyInfo();
                ObjClsCompanyInfo.GetCompanyInfo(KeyField);
                if (KeyField == 0)
                {
                    aniCustomTextCompanyName.Tag = 0;
                    aniCustomTextCompanyName.Text = "";
                    txtAddress1.Text = string.Empty;
                    txtAddress2.Text = string.Empty;
                    txtCity.Text = string.Empty;
                    txtState.Text = string.Empty;
                    txtZip.Text = string.Empty;
                    txtMobile.Text = string.Empty;
                    txtTelephone.Text = string.Empty;
                    txtFax.Text = string.Empty;
                    txtEmail.Text = string.Empty;                    
                   
                }
                else
                {
                    aniCustomTextCompanyName.Tag = ObjClsCompanyInfo.CompanyInfoID.ToString();
                    aniCustomTextCompanyName.Text = ObjClsCompanyInfo.CompanyName;
                    txtAddress1.Text = ObjClsCompanyInfo.Address1.ToString();
                    txtAddress2.Text = ObjClsCompanyInfo.Address2;
                    txtCity.Text = ObjClsCompanyInfo.City;
                    txtState.Text = ObjClsCompanyInfo.State;
                    txtZip.Text = ObjClsCompanyInfo.ZipCode;
                    txtMobile.Text = ObjClsCompanyInfo.MobileNumber;
                    txtTelephone.Text = ObjClsCompanyInfo.TelephoneNumber;
                    txtFax.Text = ObjClsCompanyInfo.FaxNumber;
                    txtEmail.Text = ObjClsCompanyInfo.Email;   
                    
                }



                functionReturnValue = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
            return functionReturnValue;
        }
        private bool ValidateName()
        {
            bool bStatus = true;
            if (aniCustomTextCompanyName.Text == "")
            {
                errorProvider1.SetError(aniCustomTextCompanyName, "Please enter the Company Name");
                return false;
            }
            if (txtAddress1.Text == "")
            {
                errorProvider1.SetError(txtAddress1, "Please enter the Address1");
                return false;
            }
            if (txtAddress2.Text == "")
            {
                errorProvider1.SetError(txtAddress2, "Please enter the Address2");
                return false;
            }
            if (txtCity.Text == "")
            {
                errorProvider1.SetError(txtCity, "Please enter the City");
                return false;
            }
            if (txtState.Text == "")
            {
                errorProvider1.SetError(txtState, "Please enter the State");
                return false;
            }
            if (txtZip.Text == "")
            {
                errorProvider1.SetError(txtZip, "Please enter 6 digit pin number");
                return false;
            }
            if (txtMobile.Text == "")
            {
                errorProvider1.SetError(txtMobile, "Please enter 10 digit mobile number");
                return false;
            }
            if (txtTelephone.Text == "")
            {
                errorProvider1.SetError(txtTelephone, "Please enter the Telephone Number");
                return false;
            }
            if (txtFax.Text == "")
            {
                errorProvider1.SetError(txtFax, "Please enter the Fax Number");
                return false;
            }
            if (txtEmail.Text == "")
            {
                errorProvider1.SetError(txtEmail, "Please enter the Emaile ID");
                return false;
            }
            ClearError();
            return bStatus;
        }
        private void ClearError()
        {
            errorProvider1.Clear();
        }

        private void aniCustomTextCompanyName_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void txtAddress1_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void txtAddress2_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void txtCity_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void txtState_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void txtZip_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void txtMobile_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void txtTelephone_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void txtFax_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void txtEmail_Validating(object sender, CancelEventArgs e)
        {
            ClearError();
        }

        private void txtZip_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) || (int)e.KeyChar == 8)
            {
                e.Handled = false;
                ClearError();
            }

            else
            {
                errorProvider1.SetError(txtZip, "Please enter 6 digit pin number");
                e.Handled = true;
            }
        }

        private void txtTelephone_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (char.IsDigit(e.KeyChar) || (int)e.KeyChar == 45 || (int)e.KeyChar == 8)
            {
                e.Handled = false;
                ClearError();
            }

            else
            {
                errorProvider1.SetError(txtTelephone, "Please enter Home Phone Number");
                e.Handled = true;
            }

        }

        private void txtMobile_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (char.IsDigit(e.KeyChar) || (int)e.KeyChar == 8)
            {
                e.Handled = false;
                ClearError();
            }

            else
            {
                errorProvider1.SetError(txtMobile, "Please enter 10 digit mobile number");
                e.Handled = true;
            }

        }

        private void txtFax_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (char.IsDigit(e.KeyChar) || (int)e.KeyChar == 45 || (int)e.KeyChar == 8)
            {
                e.Handled = false;
                ClearError();
            }

            else
            {
                errorProvider1.SetError(txtFax, "Please enter the Fax Number");
                e.Handled = true;
            }

        }


      
        

       
    }
}
